package com.converse.diamondreader.activities.findId;

import android.annotation.SuppressLint;
import android.media.Image;
import android.util.Log;
import android.util.Size;
import android.view.textclassifier.TextClassification;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.mlkit.vision.common.InputImage;
import com.google.mlkit.vision.text.Text;
import com.google.mlkit.vision.text.TextRecognition;
import com.google.mlkit.vision.text.TextRecognizer;
import com.google.mlkit.vision.text.latin.TextRecognizerOptions;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.annotation.NonNull;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageProxy;
import androidx.camera.core.Preview;
import androidx.camera.core.impl.CaptureConfig;

/**
 * Author: Administrator
 * Date: 2021/12/7
 * Project: DiamondReader
 * Description:
 */
public class FindIdModelImpl implements FindIdModel {


    @Override
    @SuppressLint("RestrictedApi")
    public ImageAnalysis buildImageAnalysisObj(int with,int height) {
        ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
                .setDefaultResolution(new Size(with, height))
                .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                .build();
        return imageAnalysis;
    }

    @SuppressLint("RestrictedApi")
    @Override
    public Preview buildPreviewObj(int with,int height) {
        return new Preview.Builder().build();
    }

    @Override
    public CameraSelector buildCameraSelectorObj() {
        return new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
    }

    @Override
    public Task<Text> processTextRecognition(ImageProxy imageProxy, OnSuccessListener<Text> onSuccess, OnFailureListener onFailure) {
        TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);
        Image mediaImage = imageProxy.getImage();
        if (mediaImage != null) {
            InputImage image = InputImage.fromMediaImage(mediaImage, imageProxy.getImageInfo().getRotationDegrees());
            Log.e("xxx", "src with="+mediaImage.getWidth()+" src height="+mediaImage.getHeight());
            Log.e("xxx", "dest with="+image.getWidth()+" dest height="+image.getHeight());
            Task<Text> task = recognizer.process(image)
                    .addOnSuccessListener(onSuccess)
                    .addOnFailureListener(onFailure);
            task.addOnCompleteListener(new OnCompleteListener<Text>() {
                @Override
                public void onComplete(@NonNull Task<Text> task) {
                    recognizer.close();
                }
            });
            return task;
        }
        return null;
    }

    Pattern mPattern = Pattern.compile("^300\\d{6}");

    @Override
    public synchronized String extractDiamondId(String text) {
        text = text
                .replace(" ", "")
                .replace("o", "0")
                .replace("O", "0")
                .replace("|", "1")
                .replace("I", "1")
                .replace("z", "2")
                .replace("Z", "2")
                .replace("s", "5")
                .replace("S", "5")
                .replace("T", "5")
                .replace("r", "5")
                .replace("b", "6")
                .replace("B", "8")
                .replace("&", "8")
                .trim();

        Matcher matcher = mPattern.matcher(text);
        String result = null;
        if (matcher.find()) {
            result = matcher.group();
//            Log.e("xxx", result);
        }
        return result;

    }


}
